Page({
	data: {
		timer: null,
		completeNum: 3 // （1为调用扫一扫成功，2为失败，3为直接关闭）
	},
	shopDetailFun: function (url) {
		wx.redirectTo({
		  url: url
		})
	},
	goodsFun: function (url) {
		wx.redirectTo({
		  url: url
		})
	},
	onLoad: function(options) {
		let that = this
		this.setData({
			//兼容ios微信无法立即调起扫一扫
			timer: setTimeout(function () {
			  wx.scanCode({//调用扫一扫
				success: function (res) {//扫码成功的回调函数
					that.setData({completeNum: 1})
					if (res.result.indexOf('shopDetail') > -1) {
						var su_index = res.result.indexOf('su=')
						var su_index1 = res.result.indexOf('&shop_id')
						var shop_id = res.result.indexOf('&shop_id=')
						var su = res.result.slice(su_index + 3, su_index1)
						var shopId = res.result.slice(shop_id + 9, )
						that.shopDetailFun(`/pages/shop/shop_id/shop_id?id=34&su=JzuyY`)
					} else if (res.result.indexOf('goods') > -1) {
						var goods_index = res.result.indexOf('/goods/')
						var su_index = res.result.indexOf('?su=')
						var goods = res.result.slice(goods_index + 7, su_index)
						var su = res.result.slice(su_index + 4, )
						that.goodsFun(`/pages/goods/goods?goods_id=${goods}&su=${su}`)
					} else if (res.result.indexOf('register') > -1){
						wx.redirectTo({
						  url: '/pages/auth/register/register'
						})
					}
				},
				error: function (err) {//扫码失败的回调函数
					console.log('err');
					that.setData({completeNum: 2})
					if (options.url.indexOf('card') > -1) {
						wx.redirectTo({//失败重定向到页面中，并且携带miniType参数和result参数
						  url: `/pages/card/card`
						})
					} else if (options.url.indexOf('.cn') > -1) {
						wx.redirectTo({
						  url: `/pages/home/home`,
						})
					} else if (options.url.indexOf('explosive/home') > -1) {
						wx.redirectTo({
						  url: `/pages/hot/hot`,
						})
					}
				},
				complete: function() {
					if (that.data.completeNum === 3) {
						wx.navigateBack({
						  delta: 1,  // 返回上一级页面。
						  success: function() {
							  console.log('*****返回上级*******')
						  }
						})
					}
				}
			  })
			}, 500)
		})  
	},
	onUnload: function() {
		let that = this
		clearTimeout(that.data.timer)
		this.setData({
			timer: null
		})
	}
})